﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DebIt.Common.Model;
using Microsoft.Phone.Controls;

namespace DebIt.View.UserControls
{
    public partial class ClientControl
    {
        #region variables

        private bool _isExpanded = true;

        #endregion


        #region constructor

        public ClientControl()
        {
            InitializeComponent();
            SetControllState(false);
            LoadIcons();
        }

        #endregion


        #region dependencyPropertys

        public static DependencyProperty ClientSourceProperty = DependencyProperty.Register("ClientSource", typeof(Client), typeof(ClientControl), null);

        public Client ClientSource
        {
            get { return (Client)GetValue(ClientSourceProperty); }
            set { SetValue(ClientSourceProperty, value); }
        }


        public static DependencyProperty EditPageNavigationProperty = DependencyProperty.Register("EditPageNavigationSource", typeof(string), typeof(ClientControl), null);

        public string EditPageNavigationSource
        {
            get { return (string)GetValue(EditPageNavigationProperty); }
            set { SetValue(EditPageNavigationProperty, value); }
        }

        #endregion


        #region commands

        public static DependencyProperty ClientDeleteCommandProperty = DependencyProperty.Register("DeleteClientCommand", typeof(ICommand), typeof(ClientControl),
                                                           null);

        public ICommand DeleteClientCommand
        {
            get { return (ICommand)GetValue(ClientDeleteCommandProperty); }
            set { SetValue(ClientDeleteCommandProperty, value); }
        }

        public static DependencyProperty LoanPaiedChangeCommandProperty = DependencyProperty.Register("LoanPaiedChange", typeof(ICommand), typeof(ClientControl),
                                                           null);

        public ICommand LoanPaiedChange
        {
            get { return (ICommand)GetValue(LoanPaiedChangeCommandProperty); }
            set { SetValue(LoanPaiedChangeCommandProperty, value); }
        }

        #endregion


        #region events

        private void BtnEditClick(object sender, RoutedEventArgs e)
        {
            var phoneApplicationFrame = Application.Current.RootVisual as PhoneApplicationFrame;
            if (EditPageNavigationSource != null && phoneApplicationFrame != null)
            {
                phoneApplicationFrame.Navigate(new Uri(EditPageNavigationSource + "?id=" + ClientSource.ClientId, UriKind.Relative));
            }
        }

        private void BtnTapTapped(object sender, System.Windows.Input.GestureEventArgs e)
        {
            SetControllState(!_isExpanded);
        }

        #endregion


        #region method

        private void SetControllState(bool state)
        {
            if (state)
            {
                lbExtend.Visibility = btnDelete.Visibility = Visibility.Visible;
            }
            else
            {
                lbExtend.Visibility = btnDelete.Visibility = Visibility.Collapsed;
            }
            _isExpanded = state;
        }
        
        private void LoadIcons()
        {
            var darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            if (darkBackgroundVisibility == Visibility.Visible)
            {
                imgEdit.Source = new BitmapImage(new Uri("../../icons/dark/edit.png", UriKind.Relative));
                imgDelete.Source = new BitmapImage(new Uri("../../icons/dark/delete.png", UriKind.Relative));
            }
            else
            {
                imgEdit.Source = new BitmapImage(new Uri("../../icons/light/edit.png", UriKind.Relative));
                imgDelete.Source = new BitmapImage(new Uri("../../icons/light/delete.png", UriKind.Relative));
            }
        }

        #endregion
    }
}
